package com.dayuanit.dy15.ebook.bookshop.controller;

import com.alibaba.fastjson.JSON;
import com.dayuanit.dy15.ebook.bookshop.dto.ResponseDTO;
import com.dayuanit.dy15.ebook.bookshop.service.OrderService;
import com.dayuanit.dy15.ebook.bookshop.vo.SettlementRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class OrderCotroller extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 结算：方案一
     *
     * 私人协议  cardId$commoId$buyNum-cardId$commoId$buyNum
     *
     * 缺点：私人协议不够开放，不通用，不易维护，不易扩展。
     * @param commondityInfo
     * @return
     */
    @RequestMapping(value = "/order/settlement1")
    public ResponseDTO settlement1(String commondityInfo) {
        System.out.println(commondityInfo);
        String[] commoditys = commondityInfo.split("-");
        for (String msg : commoditys) {
            String[] commoInfo = msg.split("\\$");
            System.out.println("cardId=" + commoInfo[0] + " commoId=" + commoInfo[1] + " buyNum=" + commoInfo[2]);
        }

        return ResponseDTO.sucess();
    }

    /**
     * 方案二：前端将参数组装成json格式，后台再将json字符串反序列化成Java对象
     *
     * 缺点：还需要自己手动的反序列化。
     * @param settlementRequestVO
     * @return
     */
    @RequestMapping(value = "/order/settlement2")
    public ResponseDTO settlement2(String settlementRequestVO) {
        System.out.println(settlementRequestVO);

        //将json数组反序列化List集合  注意json数组用parseArray反序列化
        List<SettlementRequestVO> list = JSON.parseArray(settlementRequestVO, SettlementRequestVO.class);
        for (SettlementRequestVO vo : list) {
            System.out.println(vo);
        }
        return ResponseDTO.sucess();
    }

    /**
     * 方案三：前端发送json格式的参数，后端springMVC框架自动的为参数反序列化
     * 墙裂推荐*****
     * 缺点：无
     * @param commoditys
     * @return
     */
    @RequestMapping(value = "/order/settlement3")
    public ResponseDTO settlement3(@RequestBody List<SettlementRequestVO> commoditys) {
        for (SettlementRequestVO vo : commoditys) {
            System.out.println(vo);
        }

        //将选择的购物车商品存放到sesion中
        session.setAttribute("settlement-" + getUserId(), commoditys);
        return ResponseDTO.sucess();
    }

    @RequestMapping(value = "/order/loadSettlement")
    public ResponseDTO loadSettlement() {
        List<SettlementRequestVO> vos = (List)session.getAttribute("settlement-" + getUserId());
        return ResponseDTO.sucess(orderService.listSettlement(vos));
    }


    /**
     * 创建订单
     * @param addressId 收货地址ID
     * @param payChannel  支付方式
     * @return alipay的form表单
     */
    @RequestMapping("/order/createOrder")
    public ResponseDTO createOrder(long addressId, int payChannel) {
        List<SettlementRequestVO> vos = (List)session.getAttribute("settlement-" + getUserId());
        return ResponseDTO.sucess(orderService.createOrder(getUserId(), addressId, payChannel, vos));
    }

    /**
     * 查询订单的 kkj
     * @return
     */
    @RequestMapping("/order/listOrder")
    @CrossOrigin(origins = "http://127.0.0.1:9091")//告诉服务器，当有次域名下的请求，可以放行通过，突破浏览器的同源策略限制
    public ResponseDTO listOrder() {
//        return ResponseDTO.sucess( orderService.listOrder(getUserId()));
//        return ResponseDTO.sucess(orderService.listOrder2(getUserId()));
        return ResponseDTO.sucess(orderService.listOrder3(getUserId()));
    }



}
